#include "bar.h"
const int NUM = 101;
const char LABEL = '>';

// 进度条版本1
void bar()
{
    char my_bar[NUM];
    memset(my_bar, 0, sizeof(my_bar));
    int cnt = 0;
    char buff[] = {'|', '/', '-', '\\'};
    int sz = sizeof(buff);
    while(cnt < 101)
    {
        printf("[%-100s][%-3d%%][%c]\r", my_bar, cnt, buff[cnt % sz]);
        fflush(stdout);
        my_bar[cnt] = LABEL;
        cnt++;
        usleep(50000);
    }
    printf("\n");
}

//进度条版本2
void flush_bar(double total, double cur)
{
    char my_bar[NUM];
    memset(my_bar, 0, sizeof(my_bar));
    char buff[] = {'|', '\\', '-', '/'};
    int sz = sizeof(buff);
    static int cnt = 0;
    double rate = cur * 100 / total;
    int n = (int) rate;
    int i;
    for(i = 0; i < n; ++i)
        my_bar[i] = LABEL;
    printf("[%-100s][%-5.1lf%%][%c]\r", my_bar, rate, buff[cnt % sz]);
    fflush(stdout);
    cnt++;
}
